package com.example.demo.controller;


import com.example.demo.service.FileUploadService;
import com.example.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/file")
public class FileUploadController {

@Autowired
private FileUploadService fileUploadService;

   //自己用
    @PostMapping("/upload")
    @ResponseBody
    public ResultUtil fileUpload(MultipartFile file) {
        ResultUtil resultUtil = new ResultUtil();
        try {
            resultUtil.setResultCode("200");
            resultUtil.setResultMsg("请求成功");
            Map<String, Object> map = fileUploadService.uploadFile(file);
            resultUtil.setResultData(map);
        } catch (Exception e) {
            e.printStackTrace();
            resultUtil.setResultCode("601");
            resultUtil.setResultMsg("文件上传失败");
        }
        return resultUtil;
    }


//    //一网通办用
//    @PostMapping("/uploadList")
//    @ResponseBody
//    public ResultUtil fileUploadList(MultipartFile[] fileList, TableBusiness tableBusiness) {
//        ResultUtil resultUtil = new ResultUtil();
//        try {
//            resultUtil.setResultCode("200");
//            resultUtil.setResultMsg("请求成功");
//            List<Map<String, Object>> resultList = fileUploadService.uploadFileList(fileList);
//            resultUtil.setResultData(resultList);
//        } catch (Exception e) {
//            e.printStackTrace();
//            resultUtil.setResultCode("601");
//            resultUtil.setResultMsg("文件上传失败");
//        }
//        return resultUtil;
//    }



      @GetMapping("/download")
    @ResponseBody
    public ResultUtil fileDownload(HttpServletResponse response, String fileName) {
        ResultUtil resultUtil = new ResultUtil();
        try {
            resultUtil.setResultCode("200");
            resultUtil.setResultMsg("请求成功");
            fileUploadService.downloadFile(response,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            resultUtil.setResultCode("602");
            resultUtil.setResultMsg("文件下载失败");
        }
        return (resultUtil);
    }

    @GetMapping("/downloads")
    @ResponseBody
    public ResultUtil downloadFiles(HttpServletResponse response, String fileName) {
        ResultUtil resultUtil = new ResultUtil();
        try {
            resultUtil.setResultCode("200");
            resultUtil.setResultMsg("请求成功");
            fileUploadService.downloadFile(response,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            resultUtil.setResultCode("602");
            resultUtil.setResultMsg("文件下载失败");
        }
        return (resultUtil);
    }

    @GetMapping("/downloadPdf")
    @ResponseBody
    public ResultUtil downloadPdfFile(HttpServletResponse response, String fileName) {
        ResultUtil resultUtil = new ResultUtil();
        try {
            resultUtil.setResultCode("200");
            resultUtil.setResultMsg("请求成功");
            fileUploadService.downloadPdfFile(response,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            resultUtil.setResultCode("602");
            resultUtil.setResultMsg("文件下载失败");
        }
        return resultUtil;
    }

}
